package com.zhou.controller;


import com.zhou.dto.CheckAllRequest;
import com.zhou.entity.BlanQues;
import com.zhou.entity.TheoQues;
import com.zhou.entity.TheoQuesAns;
import com.zhou.enums.IsRightEnum;
import com.zhou.mapper.BlanQuesMapper;
import com.zhou.mapper.TheoQuesMapper;
import com.zhou.service.BlanQuesService;
import com.zhou.service.impl.BlanQuesServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2023-03-24
 */
@RestController
@RequestMapping("/question/blan")
@Api(tags = "填空题接口")
public class BlanQuesController {

    @Autowired
    private BlanQuesService blanQuesService;

    @Autowired
    private BlanQuesMapper blanQuesMapper;

    @Autowired
    private TheoQuesMapper theoQuesMapper;

    @GetMapping("/count")
    @ApiOperation(value = "一个填空题有多少个空，返回记录数")
    public long getCountByQuesId(@RequestParam("ques_id") Integer quesId) {
        return blanQuesService.getCountByQuesId(quesId);
    }

    @GetMapping("/check")
    @ApiOperation(value = "判断填空题答案是否正确，前端需要传quesId和一个列表[{subId:,answer},{subId:answer:}]返回一个0,1列表")
    public List<Integer> checkAllAnswers(@RequestBody CheckAllRequest request) {
        return blanQuesService.checkAllAnswers(request.getQuesId(), request.getAnswers());
    }

    @ApiOperation(value = "新增一条填空题")
    @PutMapping("/addBlan")
    public String addQues(@RequestParam(name = "subId") int[]sub,
                          @RequestParam(name = "ans") String[]answers,
                          @RequestBody TheoQues theoQues){
        theoQuesMapper.insert(theoQues);
        int quesId = theoQues.getQuesId();
        for(int i = 0;i < sub.length;i++){
            BlanQues blanQues = new BlanQues();
            blanQues.setQuesId(quesId);
            blanQues.setSubId(sub[i]);
            blanQues.setAnswer(answers[i]);
            blanQuesMapper.insert(blanQues);
        }
        return "增加题目成功";
    }

}

